package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qingcheng.dao.BrandMapper;
import com.qingcheng.entity.PageResult;
import com.qingcheng.goods.BrandService;
import com.qingcheng.pojo.goods.Brand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Override
    public List<Brand> findAll() {
        return brandMapper.selectAll();
    }

    @Override
    public PageResult<Brand> findPage(int page, int size) {

        PageHelper.startPage(page,size);

        Page<Brand> brands = (Page<Brand>) brandMapper.selectAll();


       // System.out.println("BrandServiceImplBrands+findPage:"+brands);

        return new PageResult<Brand>(brands.getTotal(),brands.getResult());

    }
    /*
    * 条件查询
    * */

    @Override
    public List<Brand> findList(Map<String, Object> searchMap) {
      //  Example example = createExample(searchMap);

        Example example = createExample(searchMap);

        List<Brand> brands = brandMapper.selectByExample(example);


        return brands;
    }

    /*分页条件查询*/

    @Override
    public PageResult<Brand> findPge( Map<String, Object> searchMap, int page, int size) {

        //传递当前页和每页显示条数
        PageHelper.startPage(page,size);

        //调用Example方法，传递参数集合
        Example example = createExample(searchMap);

        Page<Brand> byExamples = (Page<Brand>) brandMapper.selectByExample(example);

        PageResult<Brand> brandPageResult = new PageResult<>(byExamples.getTotal(), byExamples.getResult());

        return brandPageResult;


    }

    /*根据Id查询*/
    @Override
    public Brand findById(Integer id) {
        //根据Id查询
        return brandMapper.selectByPrimaryKey(id);

    }

    //添加一个
    @Override
    public void addBrand(Brand brand) {
        brandMapper.insert(brand);
    }



    //修改一个
    @Override
    public void updateBrand( Brand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    /*根据Id删除一个*/
    @Override
    public void deleteByBrand(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }


    //抽取的条件查询对象的方法
    private Example createExample( Map<String,Object> searchMap){


        Example example = new Example(Brand.class);

        Example.Criteria criteria= example.createCriteria();

        if (searchMap!=null){

            //名称条件

            if (searchMap.get("name")!=null&&!"".equals(searchMap.get("name"))){

                criteria.andLike("name","%"+(String)searchMap.get("name"));

            }
        }
        if (searchMap!=null){

            //名称条件

            if (searchMap.get("letter")!=null&&!"".equals(searchMap.get("letter"))){

                criteria.andLike("letter","%"+(String)searchMap.get("letter"));
            }
        }

        return example;

    }




}
